package com.yidu.DailyFinalTreatment.controller;


import com.yidu.DailyFinalTreatment.entity.NetWorth;
import com.yidu.DailyFinalTreatment.entity.OperationNetWorth;
import com.yidu.DailyFinalTreatment.entity.SecuritiesNetWorth;
import com.yidu.DailyFinalTreatment.service.NetWorthService;
import com.yidu.DailyFinalTreatment.service.OperationNetWorthService;
import com.yidu.DailyFinalTreatment.service.SecuritiesNetWorthService;
import com.yidu.DailyFinalTreatment.service.TANetWorthService;
import com.yidu.StockControl.entity.TAInventory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class NetWorthController {

    @Autowired
    private NetWorthService netWorthService;//自动装配
    @Autowired
    private SecuritiesNetWorthService securitiesNetWorthService;//自动装配
    @Autowired
    private OperationNetWorthService operationNetWorthService;//自动装配

    @Resource
    private TANetWorthService taNetWorthService;//自动装配

    @RequestMapping("/selectNetWorth")
    public Map selectNetWorth(String netWorthDate){
        System.out.println("这是净值统计的查询方法！================================");
        Map map = new HashMap();
        System.out.println("netWorthDate="+netWorthDate);
        List<NetWorth> netWorths = netWorthService.selectNeyWorth(netWorthDate);

        map.put("code","0");
        map.put("msg","成功");
        map.put("count",0);
        map.put("data",netWorths);
        return map;
    }

    @ResponseBody
    @RequestMapping("/insertNetWorth")
    public Object insertNetWorth(String netWorthDate,String cashId, HttpServletRequest req) throws ParseException {
        System.out.println("这是净值统计的统计方法=======================================");
        HttpSession session = req.getSession();
        String fundId = (String) session.getAttribute("fundId");
        String cashId1 = (String) session.getAttribute("cashId");
        System.out.println(fundId);
        System.out.println(cashId1);
        cashId=cashId1;
        netWorthDate = netWorthDate.trim();
        fundId=fundId.trim();
        cashId=cashId.trim();
        //债券利息
        double num=0;
        //查询证券清算款-流入（应收）
        double num1=0;
        //查询证券清算款-流出（应付）
        double num2=0;
        //主账户TA清算款-应收
        double num3=0;
        //主账户TA清算款-应付
        double num4=0;
        //次账户TA清算款-应收
        double num5=0;
        //次账户TA清算款-应付
        double num6=0;
        //主账户管理费
        double num7=0;
        //主账户托管费
        double num8=0;
        //次账户管理费
        double num9=0;
        //次账户托管费
        double num10=0;
        //合计估值增值
        double num11=0;
        //合计总资产
        double num13=0;
        //单位资产净值
        double num14=0;

        //根据时间先删后增
        netWorthService.deleteNetWorth(netWorthDate,fundId);

        String dateTimeTwo=netWorthDate;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = df.parse(netWorthDate);
        Calendar instance = Calendar.getInstance();
        instance.setTime(parse);
        int i = instance.get(Calendar.DAY_OF_WEEK);
        i--;
        if(i==6){
            instance.add(Calendar.DATE, -1); //得到前一天
            Date time = instance.getTime();
            dateTimeTwo=df.format(time);

        }else if(i==7){
            instance.add(Calendar.DATE, -2); //得到前一天
            Date time = instance.getTime();
            dateTimeTwo=df.format(time);
        }
        dateTimeTwo=dateTimeTwo.trim();

        //查询证券模块
        List<SecuritiesNetWorth> securitiesNetWorths = securitiesNetWorthService.selectSecuritiesNetWorth(netWorthDate, fundId, dateTimeTwo);
        //查询债券利息
        List<SecuritiesNetWorth> securitiesNetWorths1 = securitiesNetWorthService.selectDebentureInterestNetWorth(netWorthDate, fundId, 3);

        //合计债券利息
        if(securitiesNetWorths1.size()!=0) {
            for (SecuritiesNetWorth securitiesNetWorth : securitiesNetWorths1) {
                if(securitiesNetWorth!=null){
                    num = num + securitiesNetWorth.getTotalPrice();
                }
            }
        }

        //合计证券清算款所有流入
        List<SecuritiesNetWorth> securitiesNetWorths2 = securitiesNetWorthService.selectSecuritiesClearingNetWorth(netWorthDate, fundId, 2, 1);
        if(securitiesNetWorths2.size()!=0){
            for (SecuritiesNetWorth securitiesNetWorth : securitiesNetWorths2) {
                if(securitiesNetWorth!=null) {
                    num1 = num1 + securitiesNetWorth.getTotalPrice();
                }
            }
        }
        //合计证券清算款所有流出
        List<SecuritiesNetWorth> securitiesNetWorths3 = securitiesNetWorthService.selectSecuritiesClearingNetWorth(netWorthDate, fundId, 2, -1);
        if(securitiesNetWorths3.size()!=0){
            for (SecuritiesNetWorth securitiesNetWorth : securitiesNetWorths3) {
                if(securitiesNetWorth!=null) {
                    num2 = num2 + securitiesNetWorth.getTotalPrice();
                }
            }
        }


        //查询现金模块及现金利息
        List<OperationNetWorth> operationNetWorths = operationNetWorthService.selectOpertionNetWorth(netWorthDate, fundId, 3);

        //递增功能ID
        int id=3;


        NetWorth netWorth1 = new NetWorth(netWorthDate, "证券", fundId, 1, -1);
        int i1 = netWorthService.insertNetWorth(netWorth1);
        if(securitiesNetWorths.size() != 0) {
            //数据块插入股票模块外层对象
            NetWorth netWorth2 = new NetWorth(netWorthDate,"股票", fundId, 2,  1);
            int i3 = netWorthService.insertNetWorth(netWorth2);
            //遍历证券模块数据集合
            if (securitiesNetWorths.size() != 0) {
                for (SecuritiesNetWorth securitiesNetWorth : securitiesNetWorths) {
                    if (securitiesNetWorth != null) {
                        //数据库插入股票板块据
                        if (securitiesNetWorth.getSecuritType().equals("股票")) {
                            int i2 = netWorthService.insertNetWorth(new NetWorth(netWorthDate, securitiesNetWorth.getSecurityName(), fundId, securitiesNetWorth.getSecurityId(), securitiesNetWorth.getSecurityNum(), securitiesNetWorth.getEndMoney(), securitiesNetWorth.getSecurityMoney(), securitiesNetWorth.getMarketValue(), securitiesNetWorth.getTotalPrice(), id, 2));
                            id++;
                        }
                    }
                }
            }
            //数据块插入债券模块外层对象
            NetWorth netWorth3 = new NetWorth(netWorthDate,"债券", fundId, id,  1);
            netWorthService.insertNetWorth(netWorth3);
            //递增功能ID
            id++;
            //遍历证券模块数据集合
            if (securitiesNetWorths.size() != 0) {
                for (SecuritiesNetWorth securitiesNetWorth : securitiesNetWorths) {
                    if (securitiesNetWorths != null) {
                        //合计总市值
                        num13 = num13 + securitiesNetWorth.getMarketValue();
                        //合计证券模块估值增值
                        num11 = num11 + securitiesNetWorth.getTotalPrice();
                        //数据库插入债券板块据
                        if (securitiesNetWorth.getSecuritType().equals("债券")) {
                            netWorthService.insertNetWorth(new NetWorth(netWorthDate, securitiesNetWorth.getSecurityName(), fundId, securitiesNetWorth.getSecurityId(), securitiesNetWorth.getSecurityNum(), securitiesNetWorth.getEndMoney(), securitiesNetWorth.getSecurityMoney(), securitiesNetWorth.getMarketValue(), securitiesNetWorth.getTotalPrice(),id,netWorth3.getProjectId()));
                            id++;
                        }
                    }
                }
            }
        }
        id++;
        //数据块插入现金模块外层对象
        NetWorth netWorth4=new NetWorth(netWorthDate,"现金",fundId,id,-1);
        netWorthService.insertNetWorth(netWorth4);
        id++;
        if(operationNetWorths.size()!=0) {
            for (OperationNetWorth operationNetWorth : operationNetWorths) {
                if(operationNetWorth!=null) {
                    //获得两个账户总余额+现金利息
                    num13 = num13 + operationNetWorth.getBalance() + operationNetWorth.getCashTotalAmount();
                    if (cashId.equals(operationNetWorth.getCashId())) {
                        //数据库插入主账户基本数据及余额
                        NetWorth netWorth5 = new NetWorth(netWorthDate,operationNetWorth.getAccounName(), fundId,  operationNetWorth.getBankAccount(), operationNetWorth.getBalance(),id, netWorth4.getProjectId());
                        netWorthService.insertNetWorth(netWorth5);
                        id++;
                        //数据库插入主账户现金利息
                        NetWorth netWorth6 = new NetWorth(netWorthDate,"存款利息", fundId, operationNetWorth.getCashTotalAmount(),  id,netWorth5.getProjectId());
                        netWorthService.insertNetWorth(netWorth6);
                        id++;
                        //数据库插入主账户债券利息
                        if(num!=0){
                            NetWorth netWorth7 = new NetWorth(netWorthDate,"债券利息", fundId, num, id, netWorth5.getProjectId());
                            netWorthService.insertNetWorth(netWorth7);
                            id++;
                        }

                        //数据库插入主账户证券清算款
                        if((num1-num2)!=0) {
                            NetWorth netWorth8 = new NetWorth(netWorthDate,"证券清算款", fundId,  num1 - num2, id, netWorth5.getProjectId());
                            netWorthService.insertNetWorth(netWorth8);
                            id++;
                        }

                        //根据账户ID查询TA应收
                        List<OperationNetWorth> operationNetWorths3 = operationNetWorthService.selectOperationTA(operationNetWorth.getCashId(), 4, 1, netWorthDate, fundId);

                        if(operationNetWorths3.size()!=0){
                            for (OperationNetWorth operationNetWorth1 : operationNetWorths3) {
                                if (operationNetWorth1 != null) {
                                    //合计TA应收
                                    num3 = num3 + operationNetWorth1.getCashTotalAmount();
                                }
                            }

                        }
                        //根据账户ID循环查询TA应付
                        List<OperationNetWorth> operationNetWorths4 = operationNetWorthService.selectOperationTA(operationNetWorth.getCashId(), 4, -1, netWorthDate, fundId);
                        if (operationNetWorths4.size() != 0) {
                            for (OperationNetWorth operationNetWorth9 : operationNetWorths4) {
                                //合计TA应付
                                if (operationNetWorth9 != null) {
                                    num4 = num4 + operationNetWorth9.getCashTotalAmount();
                                }

                            }
                        }
                        //数据库插入主账户TA清算款
                        if((num3 - num4)!=0) {
                            NetWorth netWorth9 = new NetWorth(netWorthDate, "TA清算款", fundId,  num3-num4,id, netWorth5.getProjectId());
                            netWorthService.insertNetWorth(netWorth9);
                            id++;
                        }
                        //根据主账户ID查询管理费
                        List<OperationNetWorth> operationNetWorths1 = operationNetWorthService.selectOperationCost(operationNetWorth.getCashId(), netWorthDate, fundId, 1);

                        //得到主账户管理费
                        if(operationNetWorths1.size()!=0){
                            for (OperationNetWorth operationNetWorth1 : operationNetWorths1) {
                                if (operationNetWorth1 != null) {
                                    num7 = operationNetWorth1.getCashTotalAmount();
                                }
                            }
                        }

                        //根据主账户ID查询托管费
                        List<OperationNetWorth> operationNetWorths2 = operationNetWorthService.selectOperationCost(operationNetWorth.getCashId(), netWorthDate, fundId, 2);
                        if(operationNetWorths2.size()!=0){
                            //得到主账户托管费
                            for (OperationNetWorth operationNetWorth1 : operationNetWorths2) {
                                if (operationNetWorth1 != null) {
                                    num8 = operationNetWorth1.getCashTotalAmount();
                                }
                            }

                        }
                    }
                }

            }
        }
        //再次遍历现金模块数据集合
        if(operationNetWorths.size()!=0) {
            for (OperationNetWorth operationNetWorth : operationNetWorths) {
                if(operationNetWorth!=null){
                    if (!cashId.equals(operationNetWorth.getCashId())) {
                        //数据库插入次账户基本数据及余额
                        NetWorth netWorth10 = new NetWorth(netWorthDate, operationNetWorth.getAccounName(), fundId,  operationNetWorth.getBankAccount(), operationNetWorth.getBalance(),id, netWorth4.getProjectId());
                        netWorthService.insertNetWorth(netWorth10);
                        id++;
                        //数据库插入次账户现金利息
                        NetWorth valueStatistics11 = new NetWorth(netWorthDate,"存款利息", fundId,  operationNetWorth.getCashTotalAmount(), id, netWorth10.getProjectId());
                        netWorthService.insertNetWorth(valueStatistics11);
                        id++;
                        //根据账户ID循环TA应收
                        List<OperationNetWorth> operationNetWorths3 = operationNetWorthService.selectOperationTA(operationNetWorth.getCashId(), 4, 1, netWorthDate, fundId);
                        if(operationNetWorths3.size()!=0){
                            for (OperationNetWorth operationNetWorth1 : operationNetWorths3) {
                                //合计TA应收
                                if (operationNetWorth1 != null) {
                                    num5 = num5 + operationNetWorth1.getCashTotalAmount();
                                }
                            }
                        }
                        //根据账户ID循环TA应付
                        List<OperationNetWorth> operationNetWorths4 = operationNetWorthService.selectOperationTA(operationNetWorth.getCashId(), 4, -1, netWorthDate, fundId);
                        if(operationNetWorths4.size()!=0){
                            for (OperationNetWorth operationNetWorth1 : operationNetWorths4) {
                                //合计TA应付
                                if (operationNetWorth1 != null) {
                                    num6 = num6 + operationNetWorth1.getCashTotalAmount();
                                }
                            }
                        }
                        //数据库插入次账户TA清算款
                        if((num5 - num6)!=0) {
                            NetWorth netWorth12 = new NetWorth(netWorthDate,"TA清算款",  fundId, num5-num6,id, netWorth10.getProjectId());
                            netWorthService.insertNetWorth(netWorth12);
                            id++;
                        }
                        //根据次账户ID查询管理费
                        List<OperationNetWorth> operationNetWorths1 = operationNetWorthService.selectOperationCost(operationNetWorth.getCashId(), netWorthDate, fundId, 1);
                        //得到次账户管理费
                        if(operationNetWorths1.size()!=0) {
                            for (OperationNetWorth operationNetWorth1 : operationNetWorths1) {
                                if (operationNetWorth1 != null) {
                                    num9 = operationNetWorth1.getCashTotalAmount();
                                }
                            }
                        }
                        //根据次账户ID查询托管费
                        List<OperationNetWorth> operationNetWorths2 = operationNetWorthService.selectOperationCost(operationNetWorth.getCashId(), netWorthDate, fundId, 2);
                        //得到次账户托管费
                        if(operationNetWorths2.size()!=0){
                            for (OperationNetWorth operationNetWorth1 : operationNetWorths2) {
                                if (operationNetWorth1 != null) {
                                    num10 = operationNetWorth1.getCashTotalAmount();
                                }
                            }
                        }
                    }
                }
            }

        }

        NetWorth netWorth13=new NetWorth(netWorthDate,"运营",fundId,id,-1);
        netWorthService.insertNetWorth(netWorth13);
        id++;

        //数据库插入运营模块管理费
        if((num7 + num9)!=0) {
            NetWorth netWorth14 = new NetWorth(netWorthDate,"管理费", fundId, num7+num9,id, netWorth13.getProjectId());
            netWorthService.insertNetWorth(netWorth14);
            id++;
        }
        //数据库插入运营模块托管费
        if((num8 + num10)!=0) {
            NetWorth netWorth15 = new NetWorth(netWorthDate, "托管费",fundId,num8 + num10, id,netWorth13.getProjectId());
            netWorthService.insertNetWorth(netWorth15);
            id++;
        }

        NetWorth netWorth16=new NetWorth(netWorthDate,"合计",fundId,id,-1);
        netWorthService.insertNetWorth(netWorth16);
        id++;

        //数据库插入合计模块估值增值数据
        if(num11!=0) {
            NetWorth netWorth17 = new NetWorth(netWorthDate, "估值增值",fundId, num11,id, netWorth16.getProjectId());
            netWorthService.insertNetWorth(netWorth17);
            id++;
        }
        //合计总负债
        double num12=num7+num8+num9+num10+num4+num6+num2;
        //数据库插入合计模块负债数据
        if(num12!=0) {
            NetWorth netWorth18 = new NetWorth(netWorthDate, "负债",fundId,  num12,id,netWorth16.getProjectId());
            netWorthService.insertNetWorth(netWorth18);
            id++;
        }
        //合计总资产
        num13=num13+num+num1+num3+num5;
        //数据库插入合计模块资产合计数据
        if(num13!=0) {
            NetWorth netWorth19 = new NetWorth(netWorthDate, "资产合计", fundId,  num13,id, netWorth16.getProjectId());
            netWorthService.insertNetWorth(netWorth19);
            id++;
        }
        //数据库插入合计模块资产净值数据
        if((num13-num12)!=0) {
            NetWorth netWorth20 = new NetWorth(netWorthDate,"资产净值",  fundId,  num13 - num12,id,netWorth16.getProjectId());
            netWorthService.insertNetWorth(netWorth20);
            id++;
        }
        //调用方法查询TA数量
        double sum=0;
        List<TAInventory> taInventories = taNetWorthService.selectTaNum(fundId, netWorthDate);
        if(taInventories.size()!=0){
            for (TAInventory taInventory : taInventories) {
                if(taInventory!=null){
                    sum=sum+taInventory.getNumber();
                }
            }
        }
        if(sum!=0){
            num14=(num13-num12)/sum;
        }

        //数据库插入合计模块单位资产净值数据
        if(num14!=0) {
            NetWorth netWorth21 = new NetWorth(netWorthDate,"单位净值", fundId,  num14,id,netWorth16.getProjectId());
            netWorthService.insertNetWorth(netWorth21);
        }

        //从数据库查询新增的数据
        List<NetWorth> valueStatistics = netWorthService.selectNeyWorth(netWorthDate);

        Map<String,Object> josn = new HashMap<String,Object>();
        josn.put("code", 0);
        josn.put("count",valueStatistics.size());
        josn.put("msg", "");
        josn.put("data", valueStatistics);
        return josn;
    }

}
